﻿using System;
using System.Drawing;



namespace PmfWeb.Core.Utility
{
    /// <summary>
    /// 图片处理类
    /// </summary>
    public static class ImagePlus
    {
        public static Image SetNegativeEffect(Image image, int iWidth, int iHeight)
        {
            Bitmap oldMap = (Bitmap)image;
            Bitmap newmap = new Bitmap(iWidth, iHeight);
            Color pix;
            for (int x = 0; x < iWidth; x++)
            {
                for (int y = 0; y < iHeight; y++)
                {
                    int r = 0, g = 0, b = 0;
                    pix = oldMap.GetPixel(x, y);
                    r = 255 - pix.R;
                    g = 255 - pix.G;
                    b = 255 - pix.B;
                    newmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                }
            }
            oldMap.Dispose();
            oldMap = null;
            return newmap;
        }

        /// <summary>
        /// 设置图片的底片效果
        /// </summary>
        /// <param name="image">要设置的图片对象</param>
        /// <param name="iWidth">图片宽度</param>
        /// <param name="iHeight">图片高度</param>
        /// <returns>添加效果后的图片对象</returns>
        public static Image SetCarvingEffect(Image image, int iWidth, int iHeight)
        {
            Bitmap oldMap = (Bitmap)image;
            Bitmap newmap = new Bitmap(iWidth, iHeight);
            Color pix1, pix2;
            
            for (int x = 0; x < iWidth; x++)
            {
                for (int y = 0; y < iHeight; y++)
                {
                    int r = 0, g = 0, b = 0;
                    pix1 = oldMap.GetPixel(x, y);
                    pix2 = oldMap.GetPixel(x + 1, y + 1);
                    r = Math.Abs(pix1.R - pix2.R + 128);
                    g = Math.Abs(pix1.G - pix2.G + 128);
                    b = Math.Abs(pix1.B - pix2.B + 128);
                    r = r < 0 ? 0 : (r > 255 ? 255 : r);
                    g = g < 0 ? 0 : (g > 255 ? 255 : g);
                    b = b < 0 ? 0 : (b > 255 ? 255 : b);
                    newmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                }
            }
            oldMap.Dispose();
            oldMap = null;
            return newmap;
        }


    }

    ///<summary>
    /// 图片效果枚举
    ///</summary>
    public enum ImageEffect
    {
        /// <summary>
        /// 底片效果
        /// </summary>
        Negative = 0,

        /// <summary>
        /// 浮雕效果
        /// </summary>
        Carving = 1,
    }
}
